package edu.hawaii.kukui

class KukuiCupController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def authenticateService

    def index = {

          KukuiUser user = authenticateService.userDomain()
    def userPrincipal = authenticateService.principal()
    def roundTotals = []
    def intervals = CompetitionIntervals.findAll()
    intervals.each {
      def temp = new Expando()
      temp.title = it.title
      def tempStartDate = it.startDate
      def tempEndDate = it.endDate
      def actTextTotal = ActivityTextAttempt.executeQuery("select sum(at.activity.kukuiNuts) from edu.hawaii.kukui.ActivityTextAttempt at where at.validateTimestamp between ? and ? and at.kukuiUser.id = ?", [it.startDate, it.endDate, user.id])
      def results = ActivityTextAttempt.executeQuery("select new map(at.kukuiUser.building as building, at.kukuiUser.floor as floor, sum(at.activity.kukuiNuts) as sum) from edu.hawaii.kukui.ActivityTextAttempt at where group by at.kukuiUser.building, at.kukuiUser.floor")
      def buildingResults = new Expando()

      Integer tempInt = 0

      if (!(actTextTotal == [null])) {
         tempInt += actTextTotal
      }
      temp.kukuiNutsTotal = tempInt
      temp.floorMap = results

      roundTotals.add(temp)
    }

    return [roundTotals: roundTotals, results: results]
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [kukuiCupInstanceList: KukuiCup.list(params), kukuiCupInstanceTotal: KukuiCup.count()]
    }

    def create = {
        def kukuiCupInstance = new KukuiCup()
        kukuiCupInstance.properties = params
        return [kukuiCupInstance: kukuiCupInstance]
    }

    def save = {
        def kukuiCupInstance = new KukuiCup(params)
        if (kukuiCupInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), kukuiCupInstance.id])}"
            redirect(action: "show", id: kukuiCupInstance.id)
        }
        else {
            render(view: "create", model: [kukuiCupInstance: kukuiCupInstance])
        }
    }

    def show = {
        def kukuiCupInstance = KukuiCup.get(params.id)
        if (!kukuiCupInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), params.id])}"
            redirect(action: "list")
        }
        else {
            [kukuiCupInstance: kukuiCupInstance]
        }
    }

    def edit = {
        def kukuiCupInstance = KukuiCup.get(params.id)
        if (!kukuiCupInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [kukuiCupInstance: kukuiCupInstance]
        }
    }

    def update = {
        def kukuiCupInstance = KukuiCup.get(params.id)
        if (kukuiCupInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (kukuiCupInstance.version > version) {
                    
                    kukuiCupInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'kukuiCup.label', default: 'KukuiCup')] as Object[], "Another user has updated this KukuiCup while you were editing")
                    render(view: "edit", model: [kukuiCupInstance: kukuiCupInstance])
                    return
                }
            }
            kukuiCupInstance.properties = params
            if (!kukuiCupInstance.hasErrors() && kukuiCupInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), kukuiCupInstance.id])}"
                redirect(action: "show", id: kukuiCupInstance.id)
            }
            else {
                render(view: "edit", model: [kukuiCupInstance: kukuiCupInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def kukuiCupInstance = KukuiCup.get(params.id)
        if (kukuiCupInstance) {
            try {
                kukuiCupInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiCup.label', default: 'KukuiCup'), params.id])}"
            redirect(action: "list")
        }
    }
}
